//
//  ManhuaViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/22.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "ManhuaViewController.h"
#import "ManHuaCell.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import "ManHuaModel.h"
#import <UIImageView+WebCache.h>
#import "ManHuaClassifyViewController.h"
#import "MJRefresh.h"
#import "ManHuaPlayerViewController.h"
#import "FlipSquaresNavigationController.h"

@interface ManhuaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *bigArray;
@property (nonatomic, assign) int i;


@end


@implementation ManhuaViewController
- (void)dealloc
{
    [_bigArray release];
    [_table1 release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
        self.i = 2;

    }
    return self;
}

#pragma mark -
#pragma mark 请求数据
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=index&tab=0&page=1&limit=15"];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        NSArray *bigArray = [dic objectForKey:@"list"];
        for (NSDictionary *dic in bigArray) {
//            NSLog(@"%@", dic);
            ManHuaModel *model = [[ManHuaModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)MyTableView
{
    self.table1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110) style:UITableViewStylePlain];
    //设置代理人
    _table1.dataSource = self;
    _table1.delegate = self;
    self.table1.separatorStyle = UITableViewCellStyleDefault;
    //设定tableView重用池
    [self.table1 registerClass:[ManHuaCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table1];
    [self.table1 addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.table1 addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [_table1 release];
    
    [[UIApplication sharedApplication] setStatusBarHidden:TRUE];
}
//下拉刷新点击事件
- (void)loadNewData:(id)sender
{
    [self.bigArray removeAllObjects];
    [self.table1 reloadData];
    [self startConnection];
    [self.table1.header endRefreshing];
}
//上啦加载点击事件
- (void)loadMoreData
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=index&tab=0&page=%d&limit=15",self.i];
    
//    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        NSArray *bigArray = [dic objectForKey:@"list"];
        if (bigArray.count == 0) {
            [self.table1 removeFooter];
            return;
        }
        for (NSDictionary *dic in bigArray) {
            //            NSLog(@"%@", dic);
            ManHuaModel *model = [[ManHuaModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    
    self.i += 1;
    [self.table1.footer endRefreshing];
    
}
#pragma mark -
#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [self.bigArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ManHuaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (20.0*M_PI)/180, 0.0, 0.7, 0.4);
    rotation.m33 = 0.1;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    
    
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ManHuaModel *model = [self.bigArray objectAtIndex:indexPath.row];
//    [cell.image setImage:model.pic];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil options:SDWebImageRetryFailed];
    [cell.lable1 setText:model.name];
    [cell.lable2 setText:model.lastup];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    ManhuaViewController * one = [[ManhuaViewController alloc]init];
//    
//    // 2.创建navigationController (FlipSquaresNavigationController 是继承与navigationController的)
//    FlipSquaresNavigationController * flip = [[FlipSquaresNavigationController alloc]initWithRootViewController:one];
    
    
    ManHuaPlayerViewController *player = [[ManHuaPlayerViewController alloc] init];
    ManHuaModel *model = [self.bigArray objectAtIndex:indexPath.row];
    player.iD = model.myId;
    player.episode = model.lastup;
    player.navigationItem.title = model.name;
    [self.navigationController pushViewController:player animated:YES];
    
//    CATransition *trans = [CATransition animation];
//    trans.type = @"push";
//    trans.duration = 0.6f;
//    trans.subtype = @"fromLeft";
//    trans.type = @"cameraIrisHollowOpen";
//    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    
    [player release];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    [self MyTableView];
    [self startConnection];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
